package com.googlecode.wannabet.service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.googlecode.wannabet.domain.Tournament;

public class TournamentsService implements IWannabetService {
	
	public String processRequest(HttpServletRequest request, HttpServletResponse response) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query query = new Query(Tournament.class.getSimpleName());
		PreparedQuery preparedQuery = datastore.prepare(query);
		
		StringBuilder builder = new StringBuilder();
		builder.append("<wannabet>");
		builder.append("<tournaments>");
		
		for(Entity result : preparedQuery.asIterable() ) {
			builder.append("<tournament>");
			builder.append("<id>" + result.getProperty("tournamentID") + "</id>");
			builder.append("<name>" + result.getProperty("name") + "</name>");
			builder.append("</tournament>");
		}
		builder.append("</tournaments>");
		
		builder.append("</wannabet>");

		return builder.toString();
	}

}
